package com.lsp.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import butterknife.ButterKnife;
import com.lsp.base.view.titlebar.TitleBar;
import com.lsp.base.view.titlebar.TitleBarInterface;
import com.lsp.data.net.RetrofitCallManager;
import rx.Observable;
import rx.Subscriber;

/**
 * @author com.lsp.ibxs
 * @created 16/11/8 14:01
 */
public abstract class BaseActivity extends AppCompatActivity
    implements View.OnClickListener, TitleBarInterface {
  protected String TAG = "";
  protected RetrofitCallManager.RetrofitCallManagerImpl mRetrofitCallManager;
  TitleBar titleBar;
  LinearLayout llContentContainer;
  protected LayoutInflater mLayoutInflater;

  @Override protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    TAG = this.getClass().getSimpleName();
    mLayoutInflater = LayoutInflater.from(this);
    initVariable();
    setContentView(R.layout.activity_layout);
    initRootView();
    ButterKnife.bind(this);
    mRetrofitCallManager = new RetrofitCallManager.RetrofitCallManagerImpl();
    initView();
    initData();
  }

  /**
   * 初始化
   */
  private void initRootView() {
    titleBar = (TitleBar) findViewById(R.id.title_bar);
    llContentContainer = (LinearLayout) findViewById(R.id.ll_content_container);
    if (!initTitleBar()) {
      titleBar.hiddenTitleBar();
    }
    if (getLayoutId() > 0) {
      mLayoutInflater.inflate(getLayoutId(), llContentContainer, true);
      //llContentContainer.addView(contentView, LinearLayout.LayoutParams.MATCH_PARENT,
      //    LinearLayout.LayoutParams.MATCH_PARENT);
    }
    initEmptyLayout();
  }

  protected void initEmptyLayout() {
    //仅供BaseEmptyActivity使用
  }

  /**
   * 是否显示titlebar   默认不显示  只有重写该方法  并设置titlebar 才显示
   */
  protected boolean initTitleBar() {

    return false;
  }

  protected abstract void initVariable();

  protected int getLayoutId() {
    return 0;
  }

  protected int getEmptyId() {
    return 0;
  }

  protected abstract void initView();

  protected abstract void initData();

  /**
   * 请求
   *
   * @param observable 请求
   * @param subscriber 处理
   */
  public <T> void manageRetrofitCall(Observable<T> observable, Subscriber<T> subscriber) {
    if (null != mRetrofitCallManager) {
      mRetrofitCallManager.manageRetrofitCall(observable, subscriber);
    }
  }

  @Override protected void onDestroy() {
    super.onDestroy();
    if (mRetrofitCallManager != null) {
      mRetrofitCallManager.unSubscribeAll();
      mRetrofitCallManager = null;
    }
  }

  @Override public void setTitleLeft(int imgRes, View.OnClickListener mOnClickListener) {
    if (null != titleBar) {
      titleBar.setTitleLeft(imgRes, mOnClickListener);
    }
  }

  @Override public void setTitleCenter(String text, View.OnClickListener mOnClickListener) {

    if (null != titleBar) {
      titleBar.setTitleCenter(text, mOnClickListener);
    }
  }

  @Override
  public void setTitleRight(String text, int imgRes, View.OnClickListener mOnClickListener) {

    if (null != titleBar) {
      titleBar.setTitleRight(text, imgRes, mOnClickListener);
    }
  }
}
